﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NonBinaryTree
{
    public class NonBinaryTreeNodeList<T> : List<NonBinaryTreeNode<T>>
        where T : NonBinaryTreeNode<T>
    {
        public NonBinaryTreeNodeList()
        {
            Parent = null;
        }

        public NonBinaryTreeNodeList(NonBinaryTreeNode<T> parentNode)
        {
            Parent = (T)parentNode;
        }

        public T Parent { get; set; }

        public T Add(T node, bool setParent = true)
        {
            base.Add(node);
            if (setParent)
            {
                node.Parent = Parent;
            }
            return node;
        }

        public T Add(T node, Predicate<NonBinaryTreeNode<T>> exists)
        {
            if (Parent != null && Parent.Children.Exists(exists) == true)
            {
                throw new ArgumentException();
            }

            base.Add(node);
            node.Parent = Parent;
            return node;
        }

        public NonBinaryTreeNodeList<T> AddRange(NonBinaryTreeNodeList<T> nodes, bool setParent = true)
        {
            base.AddRange(nodes);
            if (setParent)
            {
                nodes.Parent = Parent;
            }
            return nodes;
        }
    }
}
